package com.base.cn.platform.os.service.manage.group;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.dao.manage.group.SysGroupFunctionDao;
import com.base.cn.platform.os.entity.manage.function.SysFunction;
import com.base.cn.platform.os.entity.manage.group.SysGroup;
import com.base.cn.platform.os.entity.manage.group.SysGroupFunction;
import com.base.cn.platform.os.entity.manage.group.SysGroupFunctionCondition;
import com.base.cn.platform.os.entity.manage.user.SysUser;
import com.base.cn.platform.os.service.manage.function.SysFunctionBiz;
import com.base.cn.platform.os.service.manage.user.SysUserBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 部门与权限的关联Biz
 *
 * @author s.li
 * @date 2019/02/13
 */
@Service
public class SysGroupFunctionBiz extends BaseBiz<SysGroupFunction, SysGroupFunctionDao> {

    @Autowired
    private SysFunctionBiz sysFunctionBiz;
    @Autowired
    private SysGroupBiz sysGroupBiz;
    @Autowired
    private SysUserBiz sysUserBiz;

    /**
     * 获取用户部门的权限数据
     * @param userId 用户ID
     * @return List<SysGroupFunction>
     */
    public List<SysGroupFunction> findUserGroupFunctionList(BigDecimal userId){
        SysUser sysUser = sysUserBiz.findById(userId,Arrays.asList("groupId"));
        if(ObjectUtils.isNotEmpty(sysUser)){
            BigDecimal groupId = sysUser.getGroupId();
            if(DataUtil.idIsNotNull(groupId)){
                List<SysGroupFunction> groupFunctionList = this.find(" groupId = "+groupId,null,null);
                return groupFunctionList;
            }
        }
        return null;
    }

    /**
     * 保存部门权限数据
     * @param groupId 部门ID
     * @param functionIds 权限ID串
     */
    @Transactional(rollbackFor =  IllegalArgumentException.class)
    public void saveGroupFunction(BigDecimal groupId,String functionIds){
        this.deleteWhereSql(" groupId =" +groupId);
        functionIds = StringUtils.subHeadTailString(functionIds,",");
        if(StringUtils.isNotEmpty(functionIds)){
            String[] arr = functionIds.split(",");
            List<SysGroupFunction> groupFunctionList = new ArrayList<>();
            Arrays.stream(arr).forEach(functionId->{
                SysGroupFunction groupFunction = new SysGroupFunction();
                groupFunction.setGroupId(groupId);
                groupFunction.setFunctionId(new BigDecimal(functionId));
                groupFunctionList.add(groupFunction);
            });
            if(ObjectUtils.isNotEmpty(groupFunctionList)){
                this.batchSave(groupFunctionList);
            }
        }
    }

    /**
     * 查询部门编辑页面显示的权限
     * @param condition 查询条件
     * @return 权限数据
     */
    public List<SysFunction> findShowGroupFunctionList(SysGroupFunctionCondition condition){
        List<SysGroupFunction> groupFunctionList = this.findGroupFunction(condition);
        List<SysFunction> sysFunctionList = sysFunctionBiz.findAllFunctionList();
        if(ObjectUtils.isNotEmpty(sysFunctionList)){
            if(ObjectUtils.isNotEmpty(groupFunctionList)){
                Map<BigDecimal,SysGroupFunction> groupFunctionMap = groupFunctionList.stream().collect(Collectors.toMap(e->e.getFunctionId(),e->e));
                setSelectedStatus(sysFunctionList,groupFunctionMap);
            }
        }
        return sysFunctionList;
    }

    /**
     * 设置选中状态
     * @param sysFunctionList
     * @param groupFunctionMap
     */
    private void setSelectedStatus(List<SysFunction> sysFunctionList,Map<BigDecimal,SysGroupFunction> groupFunctionMap ){
        sysFunctionList.forEach(e->{
            SysGroupFunction groupFunction = groupFunctionMap.get(e.getId());
            if(groupFunction !=null){
                e.setSelected(true);
            }
            List<SysFunction> childList = e.getChildSysFunctionList();
            if(ObjectUtils.isNotEmpty(childList)){
                setSelectedStatus(childList,groupFunctionMap);
            }
        });
    }

    /**
     * 查询部门权限数据列表
     * @param condition 查询条件
     * @return List<SysGroupFunction>
     */
    public List<SysGroupFunction> findGroupFunction(SysGroupFunctionCondition condition){
        List<SysGroupFunction> sysGroupFunctionList = this.find(" groupId = "+condition.getGroupId(),null,null);
        this.setExtendedData(sysGroupFunctionList,condition);
        return sysGroupFunctionList;
    }

    /**
     * 设置扩展数据
     * @param sysGroupFunctionList
     * @param condition
     */
    private void setExtendedData(List<SysGroupFunction> sysGroupFunctionList,SysGroupFunctionCondition condition){
        if(ObjectUtils.isNotEmpty(sysGroupFunctionList)){
            if(condition.isFunctionData()){//设置权限数据
                String functionIds = sysGroupFunctionList.stream().map(e->e.getFunctionId().toString()).collect(Collectors.joining(","));
                Map<BigDecimal,SysFunction> sysFunctionMap = sysFunctionBiz.findSysFunctionMapByIds(functionIds);
                if(ObjectUtils.isNotEmpty(sysFunctionMap)){
                    sysGroupFunctionList.forEach(e->{
                        e.setSysFunction(sysFunctionMap.get(e.getFunctionId()));
                    });
                }
            }
            if(condition.isGroupData()){//设置部门数据
                String groupIds = sysGroupFunctionList.stream().map(e->e.getGroupId().toString()).collect(Collectors.joining(","));
                Map<BigDecimal, SysGroup> sysGroupMap = sysGroupBiz.findSysGroupMapByIds(groupIds);
                if(ObjectUtils.isNotEmpty(sysGroupMap)){
                    sysGroupFunctionList.forEach(e->{
                        e.setSysGroup(sysGroupMap.get(e.getGroupId()));
                    });
                }
            }
        }
    }
}
